package com.zc.mychart.model;

import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;

import java.util.ArrayList;
import java.util.List;

public class GoldTransitionEntry {

    private List<Entry> As;
    private List<Entry> Bs;

    private final int M = 35;
    private final int N = 13;

    private List<Float> m35CList;
    private List<Float> m13CList;
    List<CandleEntry> transformList;
    List<Entry> transformK;
    List<Entry> transformD;
    List<Entry> lineList;

    /**
     * AA：收盘价的35日指数移动平均
     * BB : 在AA的基础上，做13日平均
     */

    public GoldTransitionEntry(List<CandleEntry> list) {
        float mAC = 0.0f;
        float mBC = 0.0f;
        boolean lastBoolean = true;
        boolean currentBoolean = true;
        String drawKD = "";
        float mCC;
        As = new ArrayList<>();
        Bs = new ArrayList<>();
        m35CList = new ArrayList<>();
        m13CList = new ArrayList<>();
        transformList = new ArrayList<>();
        lineList = new ArrayList<>();
        transformK = new ArrayList<>();
        transformD = new ArrayList<>();

        if (list != null && list.size() > 0) {
            m35CList = getEMArsv(list);
            m13CList = getMArsv(m35CList);
            for (int i = 0; i < list.size(); i++) {
                CandleEntry entry = list.get(i);
                if (i > N) {
                    mAC = m13CList.get(i - N);
                    mBC = m13CList.get(i - N - 1);
                }
                float x = entry.getX();
                if (mBC > 0) {
                    currentBoolean = mAC > mBC;
                }

                if (currentBoolean != lastBoolean && mBC > 0) {
                    if (!currentBoolean) {
                        drawKD = "拐点";
                        transformK.add(new Entry(x, (float) (entry.getHigh() + entry.getHigh() * 0.001)));
                    } else {
                        drawKD = "拐点";
                        transformD.add(new Entry(x, (float) (entry.getLow() - entry.getLow() * 0.001)));
                    }
                }

                EntryData data = new EntryData(currentBoolean, drawKD);
                data.time = ((EntryData) entry.getData()).time;

                CandleEntry entry1;

                if (mAC >= mBC) {
                    entry1 = new CandleEntry(
                            x,
                            entry.getHigh(),
                            entry.getLow(),
                            entry.getLow(),
                            entry.getHigh(),
                            data
                    );
                } else {
                    entry1 = new CandleEntry(
                            x,
                            entry.getHigh(),
                            entry.getLow(),
                            entry.getHigh(),
                            entry.getLow(),
                            data
                    );
                }
                if(i > N){
                    lineList.add(new Entry(x,mAC));
                }
                transformList.add(entry1);

                lastBoolean = currentBoolean;
            }
        }
    }

    public List<Float> getEMArsv(List<CandleEntry> list) {
        List<Float> rsvList = new ArrayList<>();
        float ema = 0;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                float rsv = list.get(i).getClose();
                if (i == 0) {
                    ema = rsv;
                } else {
                    ema = (2 * rsv + (M - 1) * ema) / (M + 1);
                }
                rsvList.add(ema);
            }
        }
        return rsvList;
    }

    public List<Float> getMArsv(List<Float> list) {
        List<Float> rsvList = new ArrayList<>();
        if (list != null) {
            for (int i = N; i < list.size(); i++) {
                float sum = 0;
                for (int j = 0; j < N; j++) {
                    sum += list.get(i - j);
                }
                rsvList.add(sum / N);
            }
        }
        return rsvList;
    }

    public Float getMAC(List<Float> list, int days) {
        float sum = 0;
        if (list != null) {
            for (int i = 0; i < list.size() - 1; i++) {
                sum += list.get(i);
            }
        }
        return sum / days;
    }

    public List<CandleEntry> getTransformList() {
        return transformList;
    }

    public List<Entry> getTransformK() {
        return transformK;
    }

    public List<Entry> getTransformD() {
        return transformD;
    }

    public List<Entry> getLineList() {
        return lineList;
    }

}
